<template>
  <div class="app-container">

    <!--搜索框-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--空状态时的占位提示-->
      <template slot="empty">
        <el-empty>
          <el-button type="primary" @click="resetQuery">刷新试试</el-button>
        </el-empty>
      </template>
      <el-form-item label="要点名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入要点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-select v-model="queryParams.region" placeholder="请输入区域" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.showcase_region_1580"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钢种" prop="steelGrade">
        <el-input
          v-model="queryParams.steelGrade"
          placeholder="请输入钢种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.showcase_examine_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ><el-tag :type="getStateColorType(dict.value)">{{dict.label}}</el-tag></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="warning" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--表外操作按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['showcase_1580:technologyExamine1580:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['showcase_1580:technologyExamine1580:remove']"
        >批量删除</el-button>
      </el-col>
    </el-row>

<!--    数据表格-->
    <el-table v-loading="loading" :data="technologyExamineList" @selection-change="handleSelectionChange" stripe border highlight-current-row height="650">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="50" align="center">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="要点名称" align="center" prop="name" >
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.name)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.name">
              {{ formatDisplayText(scope.row.name) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="区域" align="center" prop="regionName">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.regionName)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.regionName">
              {{ formatDisplayText(scope.row.regionName) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderNo" width="160">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.orderNo)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.orderNo">
              {{ formatDisplayText(scope.row.orderNo) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="钢种" align="center" prop="steelGrade" width="160">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.steelGrade)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.steelGrade">
              {{ formatDisplayText(scope.row.steelGrade) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="宽下限" align="center" prop="widthLow" width="60"/>
      <el-table-column label="宽上限" align="center" prop="widthHigh" width="60"/>
      <el-table-column label="厚下限" align="center" prop="thicknessLow" width="60"/>
      <el-table-column label="厚上线" align="center" prop="thicknessHigh" width="60"/>
      <el-table-column label="工艺要点" align="center" prop="processS">
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :disabled="true"
            :content="formatContent(scope.row.processS)"
            placement="top-start">
            <span class="cell-content" :title="scope.row.processS">
              {{ formatDisplayText(scope.row.processS) }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.showcase_examine_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="endDate" width="180" sortable>
        <template slot-scope="scope">
          <span :style="changeColorByData(scope.row.endDate)">{{ scope.row.endDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" align="center" prop="applyDate" width="180" sortable/>
      <el-table-column label="申请人" align="center" prop="applyPerson" sortable/>
      <el-table-column label="审核日期" align="center" prop="examineDate" width="180" sortable/>
      <el-table-column label="审核人" align="center" prop="examinePerson" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="130">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['showcase_1580:technologyExamine1580:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #F56C6C"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['showcase_1580:technologyExamine1580:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺要点申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="540px" append-to-body :fullscreen="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="要点名称" prop="name">
          <el-input v-model="form.name" type="textarea" placeholder="请输入要点名称" />
        </el-form-item>
        <el-form-item label="区域">
          <el-select v-model="form.region" prop="region" placeholder="请选择区域" multiple clearable>
            <el-option
              v-for="dict in dict.type.showcase_region_1580"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="钢种" prop="steelGrade">
          <el-input v-model="form.steelGrade" type="textarea" placeholder="请输入钢种" />
        </el-form-item>
        <el-form-item label="宽度范围">
          <el-col :span="11">
            <el-input placeholder="宽下限" prop="widthLow" v-model="form.widthLow" style="width: 90%;" clearable/>
          </el-col>
          <el-col class="line" :span="2">--</el-col>
          <el-col :span="11">
            <el-input placeholder="宽上限" prop="widthHigh" v-model="form.widthHigh" style="width: 100%;" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="厚度范围">
          <el-col :span="11">
            <el-input placeholder="厚下限" prop="thicknessLow" v-model="form.thicknessLow" style="width: 90%;" clearable/>
          </el-col>
          <el-col class="line" :span="2">--</el-col>
          <el-col :span="11">
            <el-input placeholder="厚上限" prop="thicknessHigh" v-model="form.thicknessHigh" style="width: 100%;" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="工艺要点" prop="processS">
          <el-input v-model="form.processS" type="textarea" :rows="5" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item prop="endDate" label="有效期">
          <el-date-picker
            v-model="form.endDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择有效期"/>
        </el-form-item>
        <el-form-item label="申请人">
          <el-col :span="6">
            <el-form-item prop="applyPerson">
              <el-input v-model="form.applyPerson" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2"></el-col>
          <el-col :span="16">
            <el-form-item label="申请日期" prop="applyDate">
              <el-date-picker clearable
                              v-model="form.applyDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              :disabled="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTechnologyExamine, getTechnologyExamine, delTechnologyExamine, addTechnologyExamine, updateTechnologyExamine } from "@/api/showcase/showcase_1580/technologyExamine1580";
import moment from 'moment/moment'

export default {
  name: "TechnologyExamine1580",
  dicts: ['showcase_examine_state','showcase_region_1580'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺要点申请表格数据
      technologyExamineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: null,
        region: null,
        regionName: null,
        orderNo: null,
        steelGrade: null,
        state: null,
        applyPerson: sessionStorage.getItem('currentUser') === '管理员' ? null : sessionStorage.getItem('currentUser'), //当前登录用户
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工艺要点申请列表 */
    getList() {
      this.loading = true;
      listTechnologyExamine(this.queryParams).then(response => {
        this.technologyExamineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        region: null,
        regionName: null,
        orderNo: null,
        steelGrade: null,
        widthLow: null,
        widthHigh: null,
        thicknessLow: null,
        thicknessHigh: null,
        processS: null,
        state: '0',
        endDate: null,
        applyDate: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), //当前时间
        applyPerson: sessionStorage.getItem('currentUser') , //当前登录用户,
        examineDate: null,
        examinePerson: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工艺要点申请";

      //作业区域字符串转数组
      this.form.region = this.form.region === null || this.form.region.length === 0 ? [] : this.form.region.split(",");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTechnologyExamine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工艺要点申请";

        //作业区域字符串转数组
        this.form.region = response.data.region === null || response.data.region.length === 0 ? [] : response.data.region.split(",");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //regionName表单中没有赋值，根据选择的region来赋值，且后端需要regionName是字符串格式
          this.form.regionName = this.form.regionName === null || this.form.regionName.length === 0 ? [] : this.form.regionName.split(",");//作业区域中文字符串转数组
          this.form.regionName.splice(0, this.form.regionName.length);//清空数组
          this.form.region.forEach((region_item) => { //遍历区域代码数组,更新区域中文数组
              if (region_item === '1580CUZHA') this.form.regionName.push('1580粗轧');
              if (region_item === '1580JIARELU') this.form.regionName.push('1580加热炉');
              if (region_item === '1580JINGZHA') this.form.regionName.push('1580精轧');
              if (region_item === '1580JUANQU') this.form.regionName.push('1580卷取');
              if (region_item === '2050CUZHA') this.form.regionName.push('2050粗轧');
              if (region_item === '2050JIARELU') this.form.regionName.push('2050加热炉');
              if (region_item === '2050JINGZHA') this.form.regionName.push('2050精轧');
              if (region_item === '2050JUANQU') this.form.regionName.push('2050卷取');
            }
          );
          this.form.region = this.form.region.join(","); //region数组转到字符串
          this.form.regionName = this.form.regionName.join(","); //regionName数组转到字符串

          if (this.form.id != null) {
            updateTechnologyExamine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechnologyExamine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let count = typeof ids === 'string' ? 1 : ids.length; //删一条还是多条，一条ids是一个字符串，多条是id的一个map
      this.$modal.confirm('该操作会将选中的【' + count + '】条申请全部删除，是否确认？').then(function() {
        return delTechnologyExamine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    // 鼠标悬停显示格式化文字
    formatDisplayText(text) {
      if (text && text.length > 30) {
        return text.slice(0, 30) + '...';
      }
      return text;
    },
    formatContent(text) {
      return text ? text.replace(/\n/g, '<br>') : '';
    },

    //根据时间改变文字颜色
    changeColorByData(endDate) {
      const now = new Date();
      const validityEndDate = new Date(endDate);

      if (validityEndDate < now) {
        return 'color: #F56C6C';
      } else if (validityEndDate - now <= 24 * 60 * 60 * 1000) {
        return 'color: #E6A23C';
      }
      return ''
    },

    //搜索框状态颜色
    getStateColorType(value) {
      switch (value) {
        case '0':
          return 'info';
        case '1':
          return 'success';
        case '2':
          return 'danger';
        default:
          return '';
      }
    }
  }
};
</script>
<style scoped>
.cell-content { /*表格内文字防溢出 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
